package com.idbk.chargestation.bll;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.SwitchCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.idbk.chargestation.R;
import com.idbk.chargestation.bean.JsonPointsOverview;
import com.idbk.chargestation.bll.pile.PileVendors;

/**
 * 处理 充电点 过滤有关的逻辑和视图
 * @author lupc
 *
 */
public class PointFilter implements android.view.View.OnClickListener {

	private Context mContext;

	private LayoutInflater mInflater;

	private View mView;
	
	/**
	 * 传过来的过滤数据模型，只能在确定的时候才去修改他的值
	 */
	private PointFilterModel mModelOri;

	/**
	 * 充电点过滤器-输入缓存用
	 */
	private PointFilterModel mModelTemp;
	
	private LinearLayout[] mLayoutVendors = new LinearLayout[16];
	private int[] mVendorLayoutIds = new int[]{
			R.id.vendor_1,
			R.id.vendor_2,
			R.id.vendor_3,
			R.id.vendor_4,
			R.id.vendor_5,
			R.id.vendor_6,
			R.id.vendor_7,
			R.id.vendor_8,
			R.id.vendor_9,
			R.id.vendor_10,
			R.id.vendor_11,
			R.id.vendor_12,
			R.id.vendor_13,
			R.id.vendor_14,
			R.id.vendor_15,
			R.id.vendor_16,		
	};
	
	public PointFilter(Context context,PointFilterModel model){
		mContext = context;
		mInflater = LayoutInflater.from(mContext);
		
		mModelOri = model;

		mModelTemp = new PointFilterModel();
		mModelTemp.reset(mModelOri);
	}
	
	public View createView(ViewGroup parent){
		mView = mInflater.inflate(R.layout.dialog_point_filter_core, parent, false);
		for (int i = 0; i < mLayoutVendors.length; i++) {
			mLayoutVendors[i] = (LinearLayout) mView.findViewById(mVendorLayoutIds[i]);
		}
		
		mView.findViewById(R.id.textview_ok).setOnClickListener(this);
		mView.findViewById(R.id.textview_reset).setOnClickListener(this);	

		bindView();
		
		return mView;
	}
	
	public void initView(View parent){
		mView = parent;
		for (int i = 0; i < mLayoutVendors.length; i++) {
			mLayoutVendors[i] = (LinearLayout) mView.findViewById(mVendorLayoutIds[i]);
		}
		
		mView.findViewById(R.id.textview_ok).setOnClickListener(this);
		mView.findViewById(R.id.textview_reset).setOnClickListener(this);	

		bindView();
	}
	
	private void bindView(){
		final VendorBinder binder = new VendorBinder(mContext, mModelTemp,mLayoutVendors);
		SwitchCompat swich = (SwitchCompat)mView.findViewById(R.id.switch_vendors);
		if(mModelTemp.mResultExceptVendors == null || mModelTemp.mResultExceptVendors.size() < 1){
			swich.setChecked(true);
		} else {
			swich.setChecked(false);
		}
		swich.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					binder.selectAllVendors(true);
				} else {
					binder.selectAllVendors(false);
				}
			}
		});		

		setupStateText(R.id.textview_isopen,1,mModelTemp.showOnlyOpen);
		setupStateText(R.id.textview_point_idle,2,mModelTemp.showOnlyIdle);
		setupStateText(R.id.textview_weihu,3,mModelTemp.showUnderMaintenance);
		
		setupSpeedText(R.id.textview_speed_1,1,mModelTemp.showSlow);
		setupSpeedText(R.id.textview_speed_2,2,mModelTemp.showFast);
		setupSpeedText(R.id.textview_speed_3,3,mModelTemp.showSuper);
	}
	
	private void setupStateText(int resId,final int position,boolean show){		
		final TextView t = (TextView)mView.findViewById(resId);
		if(show){
			t.setTextColor(Color.rgb(8, 8, 8));
			t.setBackgroundResource(R.drawable.bg_select_box_active_sprite);
		} else {
			t.setTextColor(Color.rgb(102, 102, 102));
			t.setBackgroundResource(R.drawable.bg_select_box_active_disabled);
		}
		t.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				if(position == 1){
					if(mModelTemp.showOnlyOpen){
						mModelTemp.showOnlyOpen = false;
						t.setTextColor(Color.rgb(102, 102, 102));
						v.setBackgroundResource(R.drawable.bg_select_box_active_disabled);
					} else {
						t.setTextColor(Color.rgb(8, 8, 8));
						mModelTemp.showOnlyOpen = true;
						v.setBackgroundResource(R.drawable.bg_select_box_active_sprite);
					}
				} else if(position == 2){
					if(mModelTemp.showOnlyIdle){
						t.setTextColor(Color.rgb(102, 102, 102));
						mModelTemp.showOnlyIdle = false;
						v.setBackgroundResource(R.drawable.bg_select_box_active_disabled);
					} else {
						t.setTextColor(Color.rgb(8, 8, 8));
						mModelTemp.showOnlyIdle = true;
						v.setBackgroundResource(R.drawable.bg_select_box_active_sprite);
					}
				} else if(position == 3){
					if(mModelTemp.showUnderMaintenance){
						t.setTextColor(Color.rgb(102, 102, 102));
						mModelTemp.showUnderMaintenance = false;
						v.setBackgroundResource(R.drawable.bg_select_box_active_disabled);
					} else {
						t.setTextColor(Color.rgb(8, 8, 8));
						mModelTemp.showUnderMaintenance = true;
						v.setBackgroundResource(R.drawable.bg_select_box_active_sprite);
					}
				}									
			}
		});
	}

	private void setupSpeedText(int resId,final int position,boolean show){
		final TextView t = (TextView)mView.findViewById(resId);
		if(show){
			t.setTextColor(Color.rgb(8, 8, 8));
			t.setBackgroundResource(R.drawable.bg_select_box_active_sprite);
		} else {
			t.setTextColor(Color.rgb(102, 102, 102));
			t.setBackgroundResource(R.drawable.bg_select_box_active_disabled);
		}
		t.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				if(position == 1){
					if(mModelTemp.showSlow){
						t.setTextColor(Color.rgb(102, 102, 102));
						mModelTemp.showSlow = false;
						v.setBackgroundResource(R.drawable.bg_select_box_active_disabled);
					} else {
						t.setTextColor(Color.rgb(8, 8, 8));
						mModelTemp.showSlow = true;
						v.setBackgroundResource(R.drawable.bg_select_box_active_sprite);
					}
				} else if(position == 2){
					if(mModelTemp.showFast){
						t.setTextColor(Color.rgb(102, 102, 102));
						mModelTemp.showFast = false;
						v.setBackgroundResource(R.drawable.bg_select_box_active_disabled);
					} else {
						t.setTextColor(Color.rgb(8, 8, 8));
						mModelTemp.showFast = true;
						v.setBackgroundResource(R.drawable.bg_select_box_active_sprite);
					}
				} else if(position == 3){
					if(mModelTemp.showSuper){
						t.setTextColor(Color.rgb(102, 102, 102));
						mModelTemp.showSuper = false;
						v.setBackgroundResource(R.drawable.bg_select_box_active_disabled);
					} else {
						t.setTextColor(Color.rgb(8, 8, 8));
						mModelTemp.showSuper = true;
						v.setBackgroundResource(R.drawable.bg_select_box_active_sprite);
					}
				}				
			}
		});
	}
	
	/**
	 * 过滤器回调接口
	 * @author lupc
	 *
	 */
	public interface OnPointFilterListener {
		void onPointFilter();
	}
	
	public void setOnPointFilterListener(OnPointFilterListener l){
		mOnPointFilter = l;
	}

	private OnPointFilterListener mOnPointFilter;
	
	private void reset(){
		mModelTemp = new PointFilterModel();
		
		bindView();
	}
	
	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.textview_ok){
			//将选择的信息填充进去
			mModelOri.reset(mModelTemp);
			if(mOnPointFilter != null){
				mOnPointFilter.onPointFilter();
			}
		} else if(v.getId() == R.id.textview_reset){
			reset();
		}
	}
	

	/**
	 * 判断 该充电点 是否被过滤
	 * @param point 桩点
	 * @return 如果被过滤，则返回true
	 */
	public static boolean isPointFiltered(JsonPointsOverview point,PointFilterModel model){
		for (Integer vendorId : model.mResultExceptVendors) {
			if(point.operatorId == vendorId){
				return true;
			}
		}
		if(model.showOnlyOpen && !point.isOpening){
			return true;
		}
		if(model.showOnlyIdle && point.idleNum <= 0){
			return true;
		}
		if(!model.showUnderMaintenance && point.available == 0){
			return true;
		}

		if(!model.showSlow && point.isSlow){
			return true;
		}
		if(!model.showFast && point.isFast){
			return true;
		}
		if(!model.showSuper && point.isSuper){
			return true;
		}
		return false;
	}

	/**
	 * 过滤器数据载体
	 * @author lupc
	 *
	 */
	public static class PointFilterModel {
		private boolean showSlow;
		private boolean showFast;
		private boolean showSuper;

		/**
		 * 是否 只看有空闲 的充电点
		 */
		private boolean showOnlyIdle;
		/**
		 * 是否值看 开放的充电点
		 */
		private boolean showOnlyOpen;
		/**
		 * 是否显示 维护中的 充电点
		 */
		private boolean showUnderMaintenance;

		private List<Integer> mResultExceptVendors;

		public PointFilterModel(){
			mResultExceptVendors = new ArrayList<Integer>(16);

			showOnlyIdle = false;
			showOnlyOpen = false;
			showUnderMaintenance = true;

			showSlow = true;
			showFast = true;
			showSuper = true;
		}

		private void reset(PointFilterModel p){
			mResultExceptVendors.clear();
			mResultExceptVendors.addAll(p.mResultExceptVendors);

			showOnlyIdle = p.showOnlyIdle;
			showOnlyOpen = p.showOnlyOpen;
			showUnderMaintenance = p.showUnderMaintenance;

			showSlow = p.showSlow;
			showFast = p.showFast;
			showSuper = p.showSuper;
		}

		public static PointFilterModel copy(PointFilterModel p){
			PointFilterModel filter = new PointFilterModel();
			filter.reset(p);
			return filter;
		}

	}

	private static class VendorBinder {

		private Context mContext;

		private PileVendors mVendors;

		private LinearLayout[] mHolds;

		/**
		 * 充电点过滤器
		 */
		private PointFilterModel mModel;

		public VendorBinder(
				Context context,
				PointFilterModel model,
				LinearLayout[] holds){
			mContext = context;

			mVendors = new PileVendors(mContext);		

			mModel = model;

			mHolds = holds;

			setupView();
		}

		private void setupView() {
			for (int i = 0; i < mVendors.getFilterVendorCount(); i++) {
				bindView(i);			
			}
			//隐藏不需要的
			int index = mVendors.getFilterVendorCount();
			for (int i = index; i < mHolds.length; i++) {
				mHolds[i].setVisibility(View.INVISIBLE);
			}
		}

		private void bindView(final int i){
			final ImageView image = (ImageView)mHolds[i].findViewById(R.id.imageview_vendor);
			final TextView text = (TextView)mHolds[i].findViewById(R.id.textview_vendor);
			final int vendorId = mVendors.getFilterVendorId(i);
			if(mModel.mResultExceptVendors.contains(vendorId)){
				text.setTextColor(Color.rgb(102, 102, 102));				
				mHolds[i].setBackgroundResource(R.drawable.bg_select_box_active_disabled);
				image.setImageResource(mVendors.getFilterVendorLogo(i,false));
			} else {
				text.setTextColor(Color.rgb(8, 8, 8));
				mHolds[i].setBackgroundResource(R.drawable.bg_select_box_active_sprite);
				image.setImageResource(mVendors.getFilterVendorLogo(i,true));
			}

			mHolds[i].setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {				
					if(mModel.mResultExceptVendors.contains(vendorId)){
						mModel.mResultExceptVendors.remove(Integer.valueOf(vendorId));
						v.setBackgroundResource(R.drawable.bg_select_box_active_sprite);
						image.setImageResource(mVendors.getFilterVendorLogo(i,true));
						text.setTextColor(Color.rgb(8, 8, 8));
					} else {
						mModel.mResultExceptVendors.add(vendorId);
						v.setBackgroundResource(R.drawable.bg_select_box_active_disabled);
						image.setImageResource(mVendors.getFilterVendorLogo(i,false));						
						text.setTextColor(Color.rgb(102, 102, 102));
					}
				}
			});

			text.setText(mVendors.getFilterVendorName(i));
		}

		public void selectAllVendors(boolean b){
			if(b){
				mModel.mResultExceptVendors.clear();
			} else {
				mModel.mResultExceptVendors.clear();
				for (int id : mVendors.getPileVendorIds()) {
					mModel.mResultExceptVendors.add(id);
				}
			}
			setupView();
		}

	}


}
